{extend name="public:head"}
{block name="main"}

<table class="layui-hide" id="all" lay-filter="all" lay-data="{id: 'sousuo'}"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">删除选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>

<script type="text/html" id="img">
    <img src="{{d.path}}" style="height: 100%">
</script>

<script>
    layui.use(['table','layer','form'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        table.render({
            elem: '#all'
            ,url:'/admin/File/eachFile'
            ,toolbar: '#toolbarDemo'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{title:'缩略图', toolbar: '#img', width:150}
                ,{field:'path', title:'图片地址'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150 , align: 'center'}
            ]]
        });
        table.on('tool(all)', function(obj){
            var data = obj.data; //获得当前行数据
            var id = obj.data.category_id
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            json = JSON.stringify(obj.data)
            console.log(json);
            if(obj.event == 'del'){
                layer.confirm('真的删除行么', function(index){
                    layer.close(index);
                    var arr = Array()
                    arr[0] = obj.data
                    //向服务端发送删除指令
                    $.post("{:Url('admin/File/delFile')}",{'data': JSON.stringify(arr)},function(data){
                        console.log(data.code);
                        if(data.code == 400){
                            layer.msg('删除失败！！！请重试');
                        }
                        if(data.code == 200){
                            layer.msg('删除成功！！！');
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        }
                    })
                });
            }
        });

        //头工具栏事件
        table.on('toolbar(all)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    //layer.confirm(JSON.stringify(data), function(index){
                        layer.confirm('真的删除行么<br>'+JSON.stringify(data), function(index){
                            layer.close(index);
                            //向服务端发送删除指令
                            $.post("{:Url('admin/File/delFile')}",{'data': JSON.stringify(data)},function(data){
                                console.log(data.code);
                                if(data.code == 400){
                                    layer.msg('删除失败！！！请重试');
                                }
                                if(data.code == 200){
                                    layer.msg('删除成功！！！',{time:1.5*1000},function () {
                                        location.reload(); //删除对应行（tr）的DOM结构，并更新缓存
                                    });
                                }
                            })
                        });
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
            };
        });
    });
</script>


{/block}